package com.example.base.adapter.holder

import android.view.View
import com.example.base.adapter.XAdapter
import java.lang.Exception
import java.lang.reflect.ParameterizedType

fun<VH: RVHolder> XAdapter<*,*>.newRvHolder(itemView: View): VH {

    var vh: VH? = null

    val type = this.javaClass.genericSuperclass as ParameterizedType
    for (actualTypeArgument in type.actualTypeArguments) {
        try {
            val constructor = (actualTypeArgument as Class<*>).getDeclaredConstructor(View::class.java)
            val obj =  constructor.newInstance(itemView)
            if (obj is RVHolder) {
                vh = obj as VH
            }
        } catch (e: NoSuchMethodException  ) {
            e.printStackTrace()
        } catch (e: ClassCastException) {
            e.printStackTrace()
        }
    }
    return vh!!
}
